import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

import Layout from '@/layout'
import Login from '@/views/login'
import Index from '@/views/index'
import Permission from '@/views/permission'
import Role from '@/views/permission/role'
import PerList from '@/views/permission/list'

const Users = () => import('@/views/users')
const Goods = () => import('@/views/goods')
const GoodList = () => import('@/views/goods/list')
const Classify = () => import('@/views/goods/classify')
const GoodType = () => import('@/views/goods/type')
const AddGood = () => import('@/views/goods/add')
const Orders = () => import('@/views/orders')
const Orderslist = () => import('@/views/orders/list')
const Reports = () => import('@/views/reports')

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    title: '登录',
    component: Login
  },
  {
    path: '/',
    // name: 'layout',  // 有默认子路由就不用设置name
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        component: Index,
        meta: {
          title: '首页',
          icon: 'icon-home'
        }
      },
      {
        path: '/users',
        name: 'users',
        component: Users,
        meta: {
          title: '用户管理',
          icon: 'icon-home'
        }
      },
      {
        path: '/permission',
        component: Permission,
        name: 'permission',
        redirect: '/role',
        meta: {
          title: '权限管理',
          icon: 'icon-home'
        },
        children: [
          {
            path: '/role',
            name: 'role',
            component: Role,
            meta: {
              title: '角色列表',
              icon: 'icon-home1'
            }
          },
          {
            path: '/list',
            name: 'list',
            component: PerList,
            meta: {
              title: '权限列表',
              icon: 'icon-home2'
            }
          }
        ]
      },
      {
        path: '/goods', // 子路由路径为空，默认子路由渲染
        component: Goods,
        name: 'goods',
        redirect: '/goodlist',
        meta: {
          title: '商品管理',
          icon: 'icon-home'
        },
        children: [
          {
            path: '/goodlist',
            name: 'goodlist',
            component: GoodList,
            meta: {
              title: '商品列表',
              icon: 'icon-home1'
            }
          },
          {
            path: '/classify',
            name: 'classify',
            component: Classify,
            meta: {
              title: '分类参数',
              icon: 'icon-home2'
            }
          },
          {
            path: '/goodtype',
            name: 'goodtype',
            component: GoodType,
            meta: {
              title: '商品分类',
              icon: 'icon-home2'
            }
          },
          {
            path: '/addgood',
            name: 'addgood',
            component: AddGood,
            hidden: true,
            meta: {
              title: '添加商品',
              icon: 'icon-home2'
            }
          }
        ]
      },
      {
        path: '/orders', // 子路由路径为空，默认子路由渲染
        component: Orders,
        name: 'orders',
        redirect: '/orderslist',
        meta: {
          title: '订单管理',
          icon: 'icon-home'
        },
        children: [
          {
            path: '/orderslist',
            name: 'orderslist',
            component: Orderslist,
            meta: {
              title: '订单列表',
              icon: 'icon-home1'
            }
          }
        ]
      },
      {
        path: '/reports',
        component: Reports,
        name: 'reports',
        meta: {
          title: '数据报表',
          icon: 'icon-home'
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  // 去的页面不是 /login
  if (to.path !== '/login') {
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
